package com.shuo.service.impl;

import com.shuo.dao.UserDao;
import com.shuo.model.User;
import com.shuo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by linshuo on 2018/3/27.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional
    public long add(User user) {
        return userDao.add(user);
    }

    @Override
    @Transactional
    public long update(User user) {
        return userDao.update(user);
    }

    @Override
    @Transactional
    public long delete(long id) {
        return userDao.delete(id);
    }

    @Override
    @Transactional
    public long deleteBy(String field, Object value) {
        return userDao.deleteBy(field, value);
    }

    @Override
    public Long count() {
        return userDao.count();
    }

    @Override
    public Long countBy(String field, Object value) {
        return userDao.countBy(field, value);
    }

    @Override
    public User get(Long id) {
        return userDao.get(id);
    }

    @Override
    public User getBy(String field, Object value) {
        return userDao.getBy(field, value);
    }

    @Override
    public User getByAnd(String field1, Object value1, String field2, Object value2) {
        return userDao.getByAnd(field1, value1, field2, value2);
    }

    @Override
    public User getByOr(String field1, Object value1, String field2, Object value2) {
        return userDao.getByOr(field1, value1, field2, value2);
    }

    @Override
    public List<User> listAll() {
        return userDao.listAll();
    }

    @Override
    public List<User> listBy(String field, Object value) {
        return userDao.listBy(field, value);
    }

    @Override
    public List<User> listByAnd(String field1, Object value1, String field2, Object value2) {
        return userDao.listByAnd(field1, value1, field2, value2);
    }

    @Override
    public List<User> listByOr(String field1, Object value1, String field2, Object value2) {
        return userDao.listByOr(field1, value1, field2, value2);
    }

    @Transactional
    public void testA() {
        this.add(new User("王小明", 12));
    }

    @Transactional
    public void testB() {
        this.add(new User("李小狼", 15));
        throw new RuntimeException("Error");
    }

    @Transactional
    public void testC() {
        this.testA();
        try {
            this.testB();
        } catch (Exception e) {
        }
    }

    public void testD() {
        this.testA();
        this.testB();
    }

    @Transactional
    public void testE() {
        this.testA();
        this.testB();
    }

}
